home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Properties / Insert_Buffer_Properties.bsh next >
Encoding:
Text File  |  2004-08-29  |  7.1 KB  |  251 lines

  1. /*
  2.  * Insert_Buffer_Properties.bsh - a Beanshell macro
  3.  * for the jEdit text editor that provides a gui for
  4.  * inserting Buffer Local properties for the current buffer
  5.  * into the current buffer.  If the buffer's mode as a line
  6.  * comment defined, or comment start and end properties then
  7.  * the inserted properties will be commented out.
  8.  *
  9.  * Copyright (C) 2002, 2003 Ollie Rutherfurd <oliver@rutherfurd.net>
  10.  *
  11.  * This program is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU General Public License
  13.  * as published by the Free Software Foundation; either version 2
  14.  * of the License, or any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  *
  25.  * $Id: Insert_Buffer_Properties.bsh,v 1.5 2004/03/08 04:29:11 spestov Exp $
  26.  */
  27.  
  28. import java.awt.BorderLayout;
  29. import java.awt.event.ActionEvent;
  30. import java.util.Hashtable;
  31. import java.util.StringTokenizer;
  32. import java.util.Vector;
  33. import javax.swing.Box;
  34. import javax.swing.BoxLayout;
  35. import javax.swing.JButton;
  36. import javax.swing.JDialog;
  37. import javax.swing.JLabel;
  38. import javax.swing.JPanel;
  39. import javax.swing.JScrollPane;
  40. import javax.swing.border.EmptyBorder;
  41. import org.gjt.sp.jedit.gui.JCheckBoxList;
  42.  
  43. BufferLocalPropertiesDialog(View view){
  44.  
  45.     this.view = view;
  46.     buffer = view.getTextArea().getBuffer();
  47.     mode = buffer.getMode().name;
  48.  
  49.     props = new Hashtable();
  50.     props.put("mode","");
  51.     props.put("indentSize","int");
  52.     props.put("tabSize","int");
  53.     props.put("noTabs","bool");
  54.     props.put("indentOnTab","bool");
  55.     props.put("indentOnEnter","bool");
  56.     props.put("wrap","str");
  57.     props.put("maxLineLen","int");
  58.     props.put("folding","str");
  59.     props.put("collapseFolds","int");
  60.     props.put("lineSeparator","");
  61.     props.put("gzipped","bool");
  62.     props.put("trailingEOL","bool");
  63.     props.put("deepIndent","bool");
  64.  
  65.     _checked = jEdit.getProperty("macro.insert-buffer-properties." + mode,"");
  66.     tokens = new StringTokenizer(_checked,",");
  67.     checkedProps = new Hashtable();
  68.     while(tokens.hasMoreTokens())
  69.     {
  70.         property = tokens.nextToken();
  71.         checkedProps.put(property,"checked");
  72.     }
  73.  
  74.     dialog = new JDialog(view,"Insert Buffer Local Properties",true);
  75.     content = new JPanel(new BorderLayout());
  76.     content.setBorder(new EmptyBorder(10,10,10,10));
  77.     dialog.setContentPane(content);
  78.     content.add(new JLabel("Properties:"), BorderLayout.NORTH);
  79.  
  80.     _entries = new Vector();
  81.     names = props.keys();
  82.     while(names.hasMoreElements()){
  83.         name = names.nextElement();
  84.         checked = checkedProps.get(name);
  85.         entry = new JCheckBoxList.Entry(checked != null,name);
  86.         _entries.addElement(entry);
  87.     }
  88.     entries = new JCheckBoxList.Entry[_entries.size()];
  89.     _entries.copyInto(entries);
  90.  
  91.     checkBox = new JCheckBoxList(entries);
  92.     checkBox.addKeyListener(this);
  93.     content.add(new JScrollPane(checkBox),
  94.         BorderLayout.CENTER);
  95.  
  96.     buttons = new JPanel();
  97.     buttons.setBorder(new EmptyBorder(12,50,0,50));
  98.     buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  99.     buttons.add(Box.createGlue());
  100.     insert = new JButton("Insert");
  101.     cancel = new JButton("Cancel");
  102.     insert.addActionListener(this);
  103.     cancel.addActionListener(this);
  104.     dialog.getRootPane().setDefaultButton(insert);
  105.     buttons.add(insert);
  106.     buttons.add(Box.createHorizontalStrut(6));
  107.     buttons.add(cancel);
  108.     buttons.add(Box.createGlue());
  109.     content.add(buttons,BorderLayout.SOUTH);
  110.  
  111.     void actionPerformed(ActionEvent evt){
  112.         if(evt.getSource() == cancel)
  113.             dialog.dispose();
  114.         else
  115.             this.insertProperties();
  116.     }
  117.  
  118.     keyPressed(KeyEvent evt){
  119.         if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  120.             dialog.dispose();
  121.         else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  122.             this.insertProperties();
  123.     }
  124.     keyReleased(KeyEvent evt){;}
  125.     keyTyped(KeyEvent evt){;}
  126.  
  127.     insertProperties(){
  128.  
  129.         if(buffer.isReadOnly())
  130.         {
  131.             Macros.error(view,"Buffer is read-only");
  132.             dialog.dispose();
  133.         }
  134.  
  135.         checkNextTime = new StringBuffer();
  136.         buff = new StringBuffer();
  137.         names = checkBox.getCheckedValues();
  138.         for(i=0; i < names.length; i++)
  139.         {
  140.             if(i > 0)
  141.                 checkNextTime.append(",");
  142.             checkNextTime.append(name);
  143.  
  144.             name = names[i];
  145.             type = props.get(name);
  146.  
  147.             if(name.equals("mode"))
  148.             {
  149.                 value = mode;
  150.             }
  151.             else if(name.equals("lineSeparator"))
  152.             {
  153.                 value = buffer.getProperty(name);
  154.                 if(value.equals("\n"))
  155.                     value = "\\n";
  156.                 else if(value.equals("\r"))
  157.                     value = "\\r";
  158.                 else
  159.                     value = "\\r\\n";
  160.             }
  161.             else if(type.equals("bool"))
  162.             {
  163.                 value = buffer.getProperty(name);
  164.                 if(value == null)
  165.                     value = "false";
  166.                 else if(value.equals(""))
  167.                     value = "false";
  168.                 else if(value.equals("0"))
  169.                     value = "false";
  170.                 else if(value.equals("false"))
  171.                     value = value; // no-op
  172.                 else
  173.                     value = "true";
  174.             }
  175.             else if(type.equals("int"))
  176.             {
  177.                 value = buffer.getProperty(name);
  178.                 if(value == null)
  179.                     value = "";
  180.             }
  181.             else    // str
  182.             {
  183.                 value = buffer.getProperty(name);
  184.                 if(value == null)
  185.                     value = "";
  186.             }
  187.             buff.append(":");
  188.             buff.append(name).append("=").append(value);
  189.         }
  190.  
  191.         jEdit.setProperty("macro.insert-buffer-properties." + mode,
  192.             checkNextTime.toString());
  193.  
  194.         if(buff.length() > 0)
  195.             buff.append(":");
  196.         properties = buff.toString();
  197.  
  198.         // try to comment out the properties first using a lineComment
  199.         // and if that's not defined, look for comment start and end
  200.         // properties -- use context senstive properties
  201.         caret = view.getTextArea().getCaretPosition();
  202.         comment = buffer.getContextSensitiveProperty(caret,"lineComment");
  203.         if(comment != null && comment.length() > 0)
  204.             properties = comment + " " + properties;
  205.         else
  206.         {
  207.             commentStart = buffer.getContextSensitiveProperty(caret,"commentStart");
  208.             commentEnd = buffer.getContextSensitiveProperty(caret,"commentEnd");
  209.             if(commentStart != null && commentEnd != null)
  210.                     properties = commentStart + " " + properties + " " + commentEnd;
  211.         }
  212.         buffer.insert(caret,properties);
  213.  
  214.         line = view.getTextArea().getCaretLine();
  215.         if(line >= 10 && line < (buffer.getLineCount()-10))
  216.             Macros.message(view, "Note: Buffer Local properties must in the first or last 10 lines of a buffer to be recognized by jEdit.");
  217.  
  218.         dialog.dispose();
  219.     }
  220.  
  221.     dialog.pack();
  222.     dialog.setSize(250,350);
  223.     dialog.setLocationRelativeTo(view);
  224.     dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  225.     dialog.setVisible(true);
  226. }
  227.  
  228. if(buffer.isReadOnly())
  229.     Macros.error(view, "Buffer is read-only.");
  230. else
  231.     BufferLocalPropertiesDialog(view);
  232.  
  233. /*
  234.     Macro index data (in DocBook format)
  235.  
  236. <listitem>
  237.     <para><filename>Insert_Buffer_Properties.bsh</filename></para>
  238.     <abstract><para>
  239.     Inserts buffer-local properties into the current buffer.
  240.     </para></abstract>
  241.     <para>
  242.     If the buffer's 
  243.     mode has a line comment defined, or comment start and end
  244.     defined, the inserted properties will be commented out.
  245.     </para>
  246. </listitem>
  247.  
  248. */
  249.  
  250. // :wrap=none:collapseFolds=0:noTabs=false:lineSeparator=\n:maxLineLen=80:mode=beanshell:indentSize=8:deepIndent=false:folding=none:
  251.